package com.wm.blog_web.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TBlogDO;
import com.wm.blog_common.entity.TBlog;
import com.wm.blog_common.result.Result;
import com.wm.blog_web.controller.base.WebBaseController;
import com.wm.blog_web.query.TBlogQuery;
import com.wm.blog_web.req.BlogSaveReq;
import com.wm.blog_web.service.TBlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Optional;


/**
 * <p>
 * 博客表 前端控制器
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Slf4j
@RestController
@RequestMapping("/web/blog")
public class TBlogController extends WebBaseController {

    @Autowired
    private TBlogService tBlogService;

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TBlogDO> get(@PathVariable Long id) {
        return tBlogService.selectById(id);
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return tBlogService.deleteById(id);
    }

    /**
     * 添加
     *
     * @param tBlog
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TBlog tBlog) {
        return tBlogService.insert(tBlog);
    }

    /**
     * 修改
     *
     * @param tBlog
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TBlog tBlog) {
        return tBlogService.update(tBlog);
    }


    /**
     * 分页查询
     *
     * @param current
     * @param size
     * @param query
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TBlogDO>> page(Integer current, Integer size, TBlogQuery query, HttpServletRequest request) {
        query = Optional.ofNullable(query).orElse(new TBlogQuery());
        if(query.getIsMyBlog()){
            //只查询属于当前登录人的博客
            query.setUserId(super.currentUserId(request));
        }
        return tBlogService.page(new QueryPage(current, size), query);
    }


    /**
     * 查询阅读数最多的5篇文章
     *
     * @return
     */
    @GetMapping("/hotBlog")
    public Result hotBlog(HttpServletRequest request) {
        Long userId = super.currentUserId(request);
        return tBlogService.hotBlog(userId);
    }


    /**
     * 查询博客详情
     *
     * @return
     */
    @GetMapping("/blogDetail")
    public Result blogDetail(Long id) {
        return tBlogService.blogDetail(id);
    }


    /**
     * 新增博客
     *
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody @Valid BlogSaveReq req, HttpServletRequest request) {
        req = Optional.ofNullable(req).orElse(new BlogSaveReq());
        Long userId = super.currentUserId(request);
        req.setUserId(userId);
        return tBlogService.save(req);
    }


    /**
     * 修改博客
     *
     * @return
     */
    @PostMapping("/editBlog")
    public Result editBlog(@RequestBody @Valid BlogSaveReq req, HttpServletRequest request) {
        req = Optional.ofNullable(req).orElse(new BlogSaveReq());
        req.setUserId(super.currentUserId(request));
        return tBlogService.editBlog(req);
    }


    /**
     * 删除博客
     *
     * @return
     */
    @DeleteMapping("/deleteBlog")
    public Result deleteBlog(Long id, HttpServletRequest request) {
        BlogSaveReq req = new BlogSaveReq();
        req.setId(id);
        req.setUserId(super.currentUserId(request));
        return tBlogService.deleteBlog(req);
    }

    /**
     * 根据关键词查询分页
     * @return
     */
    @GetMapping("/esPage")
    public Result esPage(Integer current,Integer size,String keyword) {
        return tBlogService.esPage(current,size,keyword);
    }


    /**
     * 通过月份统计
     * @return
     */
    @GetMapping("/countByMonth")
    public Result countByMonth() {
        return tBlogService.countByMonth();
    }

}
